package cn.myschool.util;
import java.sql.*;
import java.util.Properties;

/**
 * @author oldliu
 * @since 1.0
 */
public class DBUtil {
//    public static final String clsName="com.mysql.jdbc.Driver";
//    public static final String url="jdbc:mysql://localhost:3306/java78?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2b8";
//    public static final String user="root";
//    public static final String pass="root1234";
    public static  String clsName;
    public static  String url;
    public static  String user;
    public static  String pass;
    private DBUtil(){
    }
    static{
        Properties p=new Properties();
        try {
            p.load(DBUtil.class.getResourceAsStream("/cn/myschool/util/jdbc.properties"));
            clsName=p.getProperty("className");
            url=p.getProperty("url");
            user=p.getProperty("user");
            pass=p.getProperty("pass");
            Class.forName(clsName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, pass);
    }

//    public static void execute(String sql)  {
//        PreparedStatement ps=null;
//        try {
//            con = getConnection();
//            con.setAutoCommit(false);
//            ps = con.prepareStatement(sql);
//            ps.executeUpdate();
//        }catch (Exception e){
//            e.printStackTrace();
//        }finally {
//            CloseAll(null,ps,con);
//        }
//    }

    public static void  close(ResultSet res, Statement sta,Connection con)  {
        close(res);
        close(sta);
        close(con);
    }
    public static void close(ResultSet rs){
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Statement st){
        if(st!=null) {
            try {
               //st.setMaxRows(0);
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Connection con){
            if(con!=null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    }
    //CUD
    public static int executeUpdate(String sql,Object...args){
        Connection connection = null;
        PreparedStatement st = null;
        try {
            connection = DBUtil.getConnection();

            st = connection.prepareStatement(sql);
            for(int i=0;i<args.length;i++)
                st.setObject(i+1,args[i]);

            return st.executeUpdate();
        } catch (SQLException e) {
            handlerEx(e);
        } finally {
            DBUtil.close(null, st, connection);
        }
        return 0;
    }

    public static void handlerEx(Exception e){
        e.printStackTrace();
        throw new RuntimeException(e);
    }

}
